package com.design.visitor.checkoutsystem;

import java.util.ArrayList;
import java.util.List;

// 商品类
interface Item {
    void accept(PaymentVisitor visitor);
}

class FoodItem implements Item {
    @Override
    public void accept(PaymentVisitor visitor) {
        visitor.visit(this);
    }
    // 其他属性和方法...
}

class ClothingItem implements Item {
    @Override
    public void accept(PaymentVisitor visitor) {
        visitor.visit(this);
    }
    // 其他属性和方法...
}

// 支付方式访问者接口
interface PaymentVisitor {
    void visit(FoodItem foodItem);
    void visit(ClothingItem clothingItem);
}

// 具体支付方式访问者类 - 现金支付
class CashPaymentVisitor implements PaymentVisitor {
    @Override
    public void visit(FoodItem foodItem) {
        // 处理食品的现金支付逻辑
    }

    @Override
    public void visit(ClothingItem clothingItem) {
        // 处理衣物的现金支付逻辑
    }
}

// 具体支付方式访问者类 - 信用卡支付
class CreditCardPaymentVisitor implements PaymentVisitor {
    @Override
    public void visit(FoodItem foodItem) {
        // 处理食品的信用卡支付逻辑
    }

    @Override
    public void visit(ClothingItem clothingItem) {
        // 处理衣物的信用卡支付逻辑
    }
}

// 结账系统类
class CheckoutSystem {
    private List<Item> items = new ArrayList<>();

    public void addItem(Item item) {
        items.add(item);
    }

    public void processPayments(PaymentVisitor paymentVisitor) {
        for (Item item : items) {
            item.accept(paymentVisitor);
        }
    }
}

class CheckoutSystemDemo {
    public static void main(String[] args) {
        FoodItem foodItem = new FoodItem();
        ClothingItem clothingItem = new ClothingItem();

        CheckoutSystem checkoutSystem = new CheckoutSystem();
        checkoutSystem.addItem(foodItem);
        checkoutSystem.addItem(clothingItem);

        CreditCardPaymentVisitor creditCardPaymentVisitor = new CreditCardPaymentVisitor();

        checkoutSystem.processPayments(creditCardPaymentVisitor);
    }
}
